package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Orders;
import com.itheima.service.OrderService;
import com.itheima.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/14 21:49
 * @Desc: 订单管理
 */
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询订单列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/order/page")
    public R<Page<Orders>> listAll(Integer page, Integer pageSize, String number,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<Orders> ordersPage = orderService.listAll(page, pageSize, number, beginTime, endTime);

        return ordersPage != null ? R.success(ordersPage) : R.error("未查询到任何信息");
    }

    /**
     * 用户端-查询订单列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/order/userPage")
    public R<Page<Orders>> listAll(Integer page, Integer pageSize) {
        Page<Orders> ordersPage = orderService.listAll(page, pageSize);

        return ordersPage != null ? R.success(ordersPage) : R.error("没有订单");
    }

    /**
     * 下单
     * @param orders
     * @return
     */
    @PostMapping("/order/submit")
    public R<String> submitOrder(@RequestBody Orders orders, HttpServletRequest request) {
        orderService.submitOrder(orders, request);
        return R.success("添加成功");
    }
}
